package server.util;

import java.util.ArrayList;

/**
 *
 * @author Michael Hanns
 *
 */
public class Utilities {

    public static String wordWrap(String input) {

        int maxLength = 79;

        if (input.length() > maxLength) {

            ArrayList<String> lines = new ArrayList<String>();
            String line = "";
            String word = "";

            int escapeCharsSkipped = 0;

            for (int c = 0; c < input.length(); c++) {
                // Deal with escape codes first
                if (input.charAt(c) == (char) 27) {
//                    word+= (char)27;
//                    c++;
                    // Add the esc code to the word until you reach the last char
                    while (input.charAt(c) != 'm') {
                        word += input.charAt(c);
                        escapeCharsSkipped++;
                        c++;
                    }
                    // Add the m
                    word += input.charAt(c);
                    escapeCharsSkipped++;
                } else if (input.charAt(c) == '\n') {
                    if (line.length() + word.length() + 1 - escapeCharsSkipped > maxLength) {
                        lines.add(line);
                        line = word;
                        word = "";
                    } else {
                        if (line.length() > 0) {
                            line += " " + word;
                        } else {
                            line += word;
                        }
                        word = "";
                    }
                    lines.add(line);
                    line = "";
                    escapeCharsSkipped = 0;
                } else if (input.charAt(c) == ' ') {
                    // If no active word, just add white space
                    if (word.length() == 0) {
                        if (line.length() + 1 - escapeCharsSkipped > maxLength) {
                            lines.add(line);
                            line = " ";
                            escapeCharsSkipped = 0;
                        } else {
                            line += " ";
                        }
                    } else {
                        if (line.length() + word.length() + 1 - escapeCharsSkipped > maxLength) {
                            lines.add(line);
                            line = "";
                            line += word;
                            word = "";
                            escapeCharsSkipped = 0;
                        } else {
                            if (line.trim().length() > 0) {
                                line += " " + word;
                            } else {
                                line += word;
                            }
                            word = "";
                        }
                    }
                } else {
                    word += input.charAt(c);
                }
            }

            // Deal with the trailing word/line
            if (word.length() > 0) {
                line += " " + word;
                lines.add(line);
            }

            // Build the lines
            String output = "";
            int c = 0;
            for (; c < lines.size() - 1; c++) {
                output += lines.get(c) + "\n";
            }
            output += lines.get(c);

            return output;
        } else {
            return input;
        }
    }
}
